<?php


namespace App\Http\Controllers\Api\V1\Manager;


use App\Http\Controllers\Controller;
use App\Models\Suggest;
use App\Models\User;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class SuggestController extends Controller
{
    public function index(Request $request)
    {
        $pageSize = $request->pageSize;
        $excel = $request->excel;
        if($excel){
            $data = Suggest::select('t1.*','t2.phone')
                ->from((new Suggest())->getTable().' as t1')
                ->leftJoin((new User())->getTable().' as t2','t1.userId','=','t2.userId')
                ->orderByDesc('t1.createTime')
                ->get()->toArray();
            $result = [['反馈内容', '反馈人手机号', '反馈时间', '反馈图片'],];
            $i = 0;
            foreach ($data as $value) {
                $result[$i + 1] = [$value['content'], $value['phone'], $value['createTime'], $value['image']];
                $i++;
            }
            $status = Excel::create("反馈列表", function ($excel) use ($result) {
                $excel->sheet('suggest', function ($sheet) use ($result) {
                    $sheet->setFontFamily('宋体');
                    $sheet->rows($result);

                });
            });
            return $status->export('xls');
        }
        $data = Suggest::select('t1.*','t2.phone')
            ->from((new Suggest())->getTable().' as t1')
            ->leftJoin((new User())->getTable().' as t2','t1.userId','=','t2.userId')
            ->orderByDesc('t1.createTime')
            ->paginate($pageSize)->toArray();
        $result['suggestList'] = $data['data'];
        $result['totalNum'] = $data['total'];
        return $this->apiResponse($result);
    }
    public function destroy($suggestId)
    {
        Suggest::destroy($suggestId);
        return $this->apiResponse(true);
    }
}